package org.i1510.common.utils.file;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;

import org.i1510.common.bean.FileBean;
import org.junit.Test;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

public class FileManager {

	/**
	 * @Author Rambo
	 * @Desc：可使用通配符读取路径文件存到List<FileBean>
	 * @param location
	 * @return
	 * @throws IOException
	 *             TODO
	 * @Version XMLManager.java,v 1.1 2013-10-8 下午6:26:24
	 */
	public List<FileBean> readFile(String location) throws IOException {
		ResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver();
		Resource[] source = resourceLoader.getResources(location);
		List<FileBean> fileList = new ArrayList<FileBean>();
		for (int i = 0; i < source.length; i++) {
			Resource resource = source[i];
			FileBean fileBean = new FileBean();
			fileBean.setFile(resource.getFile());
			fileBean.setFileName(resource.getFilename());
			fileBean.setFilePath(resource.getFile().getPath());
			System.out.println(resource.getFile().getPath());
			fileList.add(fileBean);
		}
		return fileList;
	}

	@Test
	public void mainTest() {
		String location = "classpath:/config/**/spring-quartz.xml";
		FileManager fm = new FileManager();
		try {
			List<FileBean> fileList = new ArrayList<FileBean>();
			fileList = fm.readFile(location);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main1(String[] args) {
//		String path = "/Users/rambo/Downloads/temp/";
		String path = "smb://172.18.13.79/dev/02.JAVA/";
		File file = new File(path);
		File[] tempList = file.listFiles();
		System.out.println("该目录下对象个数：" + tempList.length);
		for (int i = 0; i < tempList.length; i++) {
			if (tempList[i].isFile()) {
				System.out.println("文     件：" + tempList[i]);
			}
			if (tempList[i].isDirectory()) {
				System.out.println("文件夹：" + tempList[i]);
			}
		}
	}

	/**
	  * 读取共享文件夹下的所有文件(文件夹)的名称
	  * @param remoteUrl
	  */
	public static void getSharedFileList(String remoteUrl) {
	    SmbFile smbFile;
	    try {
	        // smb://userName:passWord@host/path/
	        smbFile = new SmbFile(remoteUrl);
	        if (!smbFile.exists()) {
	            System.out.println("no such folder");
	        } else {
	            SmbFile[] files = smbFile.listFiles();
	            for (SmbFile f : files) {
	                System.out.println(f.getName());
	            }
	        }
	    } catch (MalformedURLException e) {
	        e.printStackTrace();
	    } catch (SmbException e) {
	        e.printStackTrace();
	    }
	}
	public static void getSharedFileList(String remoteUrl,NtlmPasswordAuthentication auth) {
	    SmbFile smbFile;
	    try {
	        // smb://userName:passWord@host/path/
	        smbFile = new SmbFile(remoteUrl,auth);
	        if (!smbFile.exists()) {
	            System.out.println("no such folder");
	        } else {
	        	smbFile.connect();
	        	System.out.println("remoteFile path:" + smbFile.getPath());
	            SmbFile[] files = smbFile.listFiles();
	            for (SmbFile f : files) {
	                System.out.println(f.getName());
	            }
	        }
	    } catch (MalformedURLException e) {
	        e.printStackTrace();
	    } catch (SmbException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
//		String path = "smb://dev:dev2017@172.18.13.79/dev/02.JAVA/";
//		getSharedFileList(path);
		
		String domainip = "172.18.10.204";
		String username = "joeone\\wuhengbo";
		String password = "";
		String remoteurl = "smb://172.18.10.204/信息平台优化项目/";

    	jcifs.Config.setProperty( "jcifs.smb.lmCompatibility", "0");

//		String domainip = "172.18.13.79";
//		String username = "dev";
//		String password = "dev2017";
//		String remoteurl = "smb://172.18.13.79/dev/01.工作安排/";

		NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(domainip, username, password);  //先登录验证
//		String path1 = "smb://wuhengbo:123qwe@smb://172.18.10.204/信息平台优化项目";
		getSharedFileList(remoteurl,auth);
		
	}
}